
import 'package:dio/dio.dart';

import 'fm_http_decode.dart';
import 'fm_http_default_decode.dart';
import 'fm_http_manager.dart';
import 'fm_http_response.dart';

enum FMHttpMethodType {
  get,
  post,
  upload,
}

abstract class FMHttpRequest {

  FMHttpMethodType methodType() {
    return FMHttpMethodType.get;
  }

  String requestDomain() {
    return '';
  }

  String requestUrl() {
    return '';
  }

  String requestUploadKey() {
    return 'file';
  }

  Map<String, dynamic> requestParams() {
    return {};
  }

  List<int>? uploadData() {
    return null;
  }

  FMHttpDecode decode() {
    return FMHttpDefaultDecode.getInstance();
  }

  bool showLog() {
    return false;
  }

  CancelToken? cancelToken;

  Future<FMHttpResponse<K>> send<T, K>({T? decodeType}) async {
    cancelToken = CancelToken();
    return FMHttpManager.getInstance()
        .send<T, K>(this, decodeType, cancelToken);
  }

  cancel() {
    if (cancelToken != null) {
      cancelToken!.cancel();
    }
  }

  String method() {
    FMHttpMethodType type = methodType();
    switch (type) {
      case FMHttpMethodType.get:
        return 'GET';
      case FMHttpMethodType.post:
        return 'POST';
      case FMHttpMethodType.upload:
        return 'POST';
      default:
        return '';
    }
  }

  String url() {
    return requestDomain() + requestUrl();
  }

  String getLastPathComponent() {
    String path = requestUrl();
    if (path.contains("/")) {
      return path.substring(path.lastIndexOf("/") + 1);
    } else {
      return path;
    }
  }
}